import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/images.dart';
import 'package:library_base/res/language.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/app_update.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:get/get.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/double_tap_back_exit_app.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:module_common/page/main/main_controller.dart';
import 'package:library_base/utils/channel.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends BaseState<MainPage, MainController> {
  late List<String> images;
  late List<String> functionStr ;

  MethodChannel fromAndroidChannel = const MethodChannel("MainPageFlutter");


  MethodChannel sendAndroidChannel1 = MethodChannel("FlutterToAndroid");


  @override
  MainController registerController() => Get.find<MainController>();

  @override
  void onCreate() {
    super.onCreate();
    setNavBarLeftVisible(false);
    setTitle(MultiLanguage.function.tr);
    fromAndroidChannel.setMethodCallHandler(flutterMethod);
  }

  @override
  void onResume() {
    super.onResume();
    baseGetxController?.requestData();
  }

  @override
  void frameCallback(Duration timeStamp) {
    initXUpdate();
  }

  Future<dynamic> flutterMethod(MethodCall methodCall) async{
    switch (methodCall.method) {
      case TO_WMS_METHOD_CHANNEL:
        Get.toNamed(Routers.wms);
        break;
    }
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    functionStr = mainPageMessages;
    images = mainPageImages;
    return DoubleTapBackExitApp(
      child: Column(
        children: [
          SizedBox(height: 50.h),
          Container(
            margin: GenerateWidgetUtil.pageHGap(),
            width: double.infinity,
            child: Padding(
              padding: EdgeInsets.zero,
              child: Text('${MultiLanguage.loginInformation.tr}: ${SPUtil.nickname}',
                  style: TextStyles.textBlack30),
            ),
          ),
          Gaps.vGap44,
          Expanded(
            child: GridView(
              padding: GenerateWidgetUtil.pageHGap(),
              children: _generateItem(),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                childAspectRatio: 0.7,
              ),
            ),
          ),
        ],
      ),
    );
  }

  List<Widget> _generateItem() {
    List<Widget> items = [];
    for (int i = 0; i < images.length; i++) {
      items.add(imageWithTextMain(images[i], functionStr[i], onTap: action));
    }
    return items;
  }

  void action(String action) {
    if (action == MultiLanguage.supplierQuotationEntry.tr) {
      ToastUtil.normal(MultiLanguage.featureNotYetAvailable.tr);
    } else if (action == MultiLanguage.supplierQuotationEntry.tr) {
      ToastUtil.normal(MultiLanguage.featureNotYetAvailable.tr);
    } else if (action == MultiLanguage.orderManager.tr) {
      Get.toNamed('/workbench');
    } else if (action == MultiLanguage.dispatchCenter.tr) {
      Get.toNamed(Routers.dispatcher);
    } else if (action == MultiLanguage.mobileReimbursement.tr) {
      ToastUtil.normal(MultiLanguage.featureNotYetAvailable.tr);
    } else if (action == MultiLanguage.biAnalysis.tr) {
      ToastUtil.normal(MultiLanguage.featureNotYetAvailable.tr);
    } else if (action == MultiLanguage.setUp.tr) {
      Get.toNamed(Routers.SET_UP_PAGE);
    } else if (action == MultiLanguage.wms.tr) {
      if(GetPlatform.isAndroid){
        sendAndroidChannel1.invokeMethod(CHECK_PERMISSION_RESULT);
      }else{
        Get.toNamed(Routers.wms);
      }
    }
  }

  @override
  void onDestroy() {
    super.onDestroy();
    sendAndroidChannel1.invokeMethod(DISSMISS_CONNECT);
  }

}



